package com.eliansun.crawler.pageparse.guangdon;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import net.dongliu.requests.Requests;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Request;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.processor.PageProcessor;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author ZW
 * @description 药品价格315网药品信息爬虫
 * @create 2020/4/24
 */

@Slf4j
public class Yao315W implements PageProcessor {


    private static final String URL = "https://www.315jiage.cn/";

    private static final String XQURL = "https://www.315jiage.cn/ajaxSuppliers.aspx";

    @SneakyThrows
    @Override
    public void process(Page page) {
        String url = page.getUrl().toString();//网址
        if (url.equals(URL)) {
            List<String> all = page.getHtml().xpath("//div[@id='middle']//div[1]//div[@class='area-sub']//div[@class='mod']//div[2]//a//@href").all();
            List<String> collect = all.stream()
                    .filter(e -> !(e.equals(URL + "c118.aspx") || e.equals(URL + "c5.aspx")
                            || e.equals(URL + "c131.aspx") || e.equals(URL + "c135.aspx")
                            || e.equals(URL + "c136.aspx") || e.equals(URL + "c169.aspx")
                            || e.equals(URL + "c199.aspx")))
                    .distinct()
                    .collect(Collectors.toList());
            //控制首页进入次页的链接:collect.size()
            for (int i = 0; i < 1; i++) {
                String aurl = collect.get(i);
                Request request = new Request();
                request.setUrl(aurl);
                page.addTargetRequest(request);
            }
        } else if (url.contains("htm")) {
            //详情页
            String productCompany = "";//生产企业
            String medicalDeviceNo = "";//医疗器械注册证编号
            String productName = "";//药品名称
            String spec = "";//规格
            String price = "";//价格
            String brand = "";//品牌
            String executiveStandard = "";//执行标准
            String nationality = "";//国产进口
            String store = "";//卖家
            String version = "";//型号
            Map<String, Object> resultMap = new HashMap<>();
            Integer cid = (Integer) page.getRequest().getExtra("cid");
            String id = getid(url);
            Map<String, String> mappam = new HashMap<>();
            mappam.put("id", "" + id);
            mappam.put("cid", "" + cid);
            String toText = Requests.get(XQURL).params(mappam).send().readToText();
            Document parse2 = Jsoup.parse(toText);
            Element prGridFoot1 = parse2.getElementById("prGridFoot");
            try {
                store = prGridFoot1.parentNode().childNodes().get(2).childNodes().get(1).childNodes().get(0).childNodes().get(0).toString();
            } catch (Exception e) {
                store = "";
            }
            try {
                price = prGridFoot1.childNodes().get(1).childNodes().get(1).childNodes().get(3).childNodes().get(0).toString();
            } catch (Exception e) {
                price = "";
            }
            productName = page.getHtml().xpath("//div[@id='tab1']//ul//li[1]//text()").get();
            spec = page.getHtml().xpath("//div[@id='content']//p[3]//u[1]//text()").get();
            productCompany = page.getHtml().xpath("//div[@id='content']//p[5]//u[1]//text()").get();
            medicalDeviceNo = page.getHtml().xpath("//div[@id='content']//p[4]//u[1]//a//text()").get();
            resultMap.put("spec", spec);//
            resultMap.put("medicalDeviceNo", medicalDeviceNo);//
            resultMap.put("productCompany", productCompany);//
            resultMap.put("price", price);
            resultMap.put("brand", brand);
            resultMap.put("productName", productName);
            resultMap.put("executiveStandard", executiveStandard);
            resultMap.put("nationality", nationality);
            resultMap.put("store", store);
            resultMap.put("url", url);
            resultMap.put("version", version);
            page.putField("result", resultMap);
        } else if (ifUrl(url)) {
            String str = page.getHtml().xpath("//div[@class='pager']//ul//li[1]//span//text()").get();
            int BNum = Integer.parseInt(str.substring(str.indexOf("/") + 1));
            //控制第一次进入次页的其他翻页链接:BNum
            for (int i = 1; i <=5; i++) {
                StringBuffer sb = new StringBuffer(url);
                sb.insert(sb.indexOf(".aspx"), "p" + i);
                String burl = sb.toString();
                Integer cid = getCid(url);
                Map<String, Object> map = new HashMap<>();
                map.put("cid", cid);
                Request request = new Request();
                request.setUrl(burl);
                request.setExtras(map);
                page.addTargetRequest(request);
            }
        } else if (!ifUrl(url)) {
            Object cid = page.getRequest().getExtra("cid");
            List<String> districts = page.getHtml().xpath("//div[@class='wrapper']//div[@class='sCard']//div[2]//a//@href").all();
            //控制每页能进入详情页的数量:districts.size()
            for (int i = 0; i < districts.size(); i++) {
                String curl = districts.get(i);
                Map<String, Object> map = new HashMap<>();
                map.put("cid", cid);
                Request request = new Request();
                request.setUrl(curl);
                request.setExtras(map);
                page.addTargetRequest(request);
            }
        }
    }

    //处理url得到id
    private String getid(String str) {
        String[] split3 = str.split("/");
        str = split3[4];
        String[] split4 = str.split("\\.");
        return split4[0];
    }

    //处理url得到cid
    private Integer getCid(String str) {
        String[] split = str.split("\\.");
        str = split[2];
        String[] split1 = str.split("/c");
        return Integer.parseInt(split1[1]);
    }

    //判断是否由首页进入
    private boolean ifUrl(String str) {
        String s = "p";
        int result = 0;
        while (str.contains(s)) {
            str = str.substring(str.indexOf(s) + 1);
            result++;
        }
        return result == 2;
    }

    private Site site = Site.me()
            .setCycleRetryTimes(5)
            .setRetryTimes(5)
            .setSleepTime(1000)
            .setTimeOut(3 * 60 * 1000)
            .setUserAgent("Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36")
            .addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8")
            .addHeader("Accept-Language", "zzh-CN,zh;q=0.8")
            .addHeader("Accept-Encoding", "zh-CN,zh;q=0.8");

    @Override
    public Site getSite() {
        return site;
    }

    public static void main(String[] args) {
        Spider.create(new Yao315W())
                .addUrl(URL)
                //设置多个线程同时抓取数据
                .thread(1)
                .run();
    }
}
